<?php
namespace Admin\Controller;
class AdminController extends BaseController{
	public function add()
	{
		//echo '<pre>';
		//var_dump($roleData);die();
		if(IS_POST)
		{
			//var_dump(I('post.'));die();
			$model = D('Admin');
			if($model->create(I('post.'), 1))
			{
				if($model->add())
				{
					$this->success('添加成功！', U('lst'));
					exit;
				}
			}
			$this->error($model->getError());
		}
		$roleModel = D('Role');
		$roleData = $roleModel->select();
		$this->assign(array(
				'roleData'=>$roleData,
		));
		$this->display();
		
   }
   public function lst(){
   	$adminModel = D('Admin');

      $adminData = array();
   	$adminData = $adminModel->select();
      $demo = $adminModel->search(5);
      // var_dump($demo);
      

      $this->assign($demo);


   	$this->display();
   }
   //删除
   public function delete(){
	   	$id = I('get.id');
	   	//var_dump($id);die();
	   	$adminModel = D('admin');
	   	//var_dump($roleModel);die();
	   	if($adminModel->delete($id)){
	   		$this->success('删除成功',U('lst'));
	   	}else{
	   		$error = $adminModel->getError();
	   		$this->error($error);
	   	}
   	
   }
   //修改
   public function edit(){
   	  $id = I('get.id');
   	  $adminModel = D('admin');
   	  if(IS_POST){
   	  	//var_dump(I('post.'));die();
   	  	//接收表单的数据  1是添加 2是修改
   	  	if($adminModel->create(I('post.'),2)){
   	     
   	  		//将表单中的数据插入到数据库
   	  		if($adminModel->save() !== FALSE){
   	  			$this->success('修改成功',U('lst'));
   	  			exit;
   	  		}
   	  	}
   	  	//模型的方法是getError
   	  	$error = $adminModel->getError();
   	  	$this->error($error);
   	  }
   	  //获取单条消息
   	
   	  $info = $adminModel->find($id);
   	 // var_dump($info);die();
   	  $a_r_model = D('admin_role');
   	  $data =	$a_r_model->field('GROUP_CONCAT(role_id) rids')->where(array(
   	  	'admin_id'=>array('eq',$id),
   	  ))->find();
   	  
   	 // echo '<pre>';
   	  //var_dump($data);die();
   	  //var_dump($data['rids']);die();
   	  $roleModel = D('Role');
   	  $roleData = $roleModel->select();
   	  $this->assign(array(
   	  		'info'=>$info,
   	  		'roleData'     =>$roleData,
   	  		'data' => $data['rids'],
   	  ));
   	  $this->display();
   	  
   	 
   }


   /*public function testTable($page=1,$pagesize=20)
   {
      $adminModel = D('Admin');
      $recordnum = $adminModel->count();
      //计算分页
      $pagenum = $recordnum / $pagesize;
      //如果不能整除，则自动加1页
      if($pagenum % 1 !== $pagenum){
      $pagenum = (int) $pagenum+1;
      }

      //利用page函数。来进行自动的分页
      $data = $adminModel->page($page,$pagesize)->select();
      $this->data = $data;
      $this->pagenum = $pagenum;
      $this->page = $page;
      $this->pagesize = $pagesize;
      $this->display('lst');
        
    } */
    public function loginout(){
    	$id = session('id');
    	$adminModel = D('Admin');
          session(null);
        $this->redirect('Login/login');
         // redirect(U('login'));
          
    }
  
}